﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Data;
using конфигуратор;

namespace Wpfконфигуратор
{


    [ValueConversion(typeof(Assembly), typeof(string))]
    public class AssemblyToString   : IValueConverter 
    {

        public object Convert ( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
            return value!=null? value.ToString():"null";
        }

        public object ConvertBack ( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
           var ff=   AppDomain.CurrentDomain.GetAssemblies().ToList();
            if (value!=null)
            return ff.FirstOrDefault(ass => (string) value == ass.FullName);
            return null;
        }

    }

    [ValueConversion(typeof(Type),typeof(string))]
    public class TypeToString : IValueConverter
    {
        

        public object Convert ( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
            return value != null ? value.ToString() : "null";
        }

        public object ConvertBack ( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
            return null;
        }

    }
     [ValueConversion(  typeof( string ),typeof(Visibility) )]
    public class StringToVisibility : IValueConverter
    {


        public object Convert ( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
            if ( String.IsNullOrEmpty( ( string ) value ) ) return Visibility.Collapsed;
            return Visibility.Visible;
        }

        public object ConvertBack ( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
            throw new NotImplementedException();
        }

    }

    [ValueConversion( typeof( object ), typeof( string ) )]
    public class ObjectToString : IValueConverter
    {


        public object Convert ( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
            return value != null ? value.ToString() : "null";
        }

        public object ConvertBack ( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
            return value;
            //return Парсер.Преобразовать((string)value, (Type) parameter);
        }


        #region Члены IMultiValueConverter

        public object Convert ( object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
            return values[0] != null ? values[0].ToString() : "null";
        }

        public object[] ConvertBack ( object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture )
        {

            return null;
        }

        #endregion
    }

}
